package day_2022_4_to_7.leetcode;

import java.util.ArrayList;

/**
 * Z 字形变换
 * @author haomin
 * @date 2022/05/21 20:19
 **/
public class Num6_sortByZhi {
    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING",3));
    }
    public static String convert(String s, int numRows) {
        ArrayList<String> list = new ArrayList<>();
        if(s.length() == 0 || s == null){
            return new String("");
        }
        if(s.length() == 1) return s;
        for (int i = 0; i < s.length(); i++) {
            int ans = i / (numRows-1);
            int cur = i % (numRows-1);
            if(ans % 2 == 0){  //结果为偶数或0
                String str = list.get(cur) == null ? new String(""):list.get(cur);
                list.set(cur,str+=s.charAt(i));
            }
            if(ans % 2 != 0){  //结果为奇数
                String str = list.get(numRows-cur-1);
                list.set(cur,str+=s.charAt(i));
            }
        }
        String result = "";
        for(int i = 0; i < list.size(); i++){
            result+=list.get(i);
        }
        return result;
    }
}